package com.wm.task.client.dao;

import com.wm.task.common.util.Command;
import com.wm.task.common.util.Const;
import com.wm.task.common.vo.Student;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户端DAO通过net来交互实现数据持久化
 */
public class StudentNetDAO {
    private static StudentNetDAO instance;

    private StudentNetDAO() {

    }

    public static StudentNetDAO getInstance() {
        if (instance == null) {
            instance = new StudentNetDAO();
        }
        return instance;
    }

    public List<Student> findAll() {
        try {
            Socket socket = new Socket(Const.SERVER, Const.SERVER_PORT);
            OutputStream os = socket.getOutputStream();

            //发指令
            os.write(Command.LIST);
            os.flush();
            //System.out.println("客户端发送："+Command.LIST);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            return (List<Student>) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public void save(Student student) {
        try {
            Socket socket = getSocket();
            OutputStream os = socket.getOutputStream();

            os.write(Command.ADD);
            os.flush();

            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(student);
            os.flush();
            oos.close();
            os.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean delete(Student student) {
        boolean bl = false;
        try {
            Socket socket = getSocket();
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();

            os.write(Command.DELETE);
            os.flush();

            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(student);
            os.flush();

            ObjectInputStream ois = new ObjectInputStream(is);
            bl = (Boolean) ois.readObject();

            ois.close();
            oos.close();
            os.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bl;
    }

    public boolean check(String name) {
        List<Student> studentList = findAll();
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().equals(name)) {
                return true;
            }
        }
        return false;

    }

    private Socket getSocket() {
        try {
            Socket socket = new Socket(Const.SERVER, Const.SERVER_PORT);
            return socket;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public Student findName(String name) {
        List<Student> studentList = findAll();
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().equals(name)) {
                return studentList.get(i);
            }
        }
        return null;
    }

    public List<Student> findBirthday(LocalDate birthdayStart, LocalDate birthdayEnd) {
        List<Student> studentList = findAll();
        List<Student> resultList = new ArrayList<>();
        for (int i = 0; i < studentList.size(); i++) {
            int s1 = studentList.get(i).getBirthday().compareTo(birthdayStart);
            int s2 = studentList.get(i).getBirthday().compareTo(birthdayEnd);
            if (s1 >= 0 && s2 <= 0) {
                resultList.add(studentList.get(i));
            }
        }
        return resultList;
    }

    public boolean handleDelete(String name) {
        List<Student> studentList = findAll();
        boolean bl = false;
        System.out.println(studentList.size());
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().equals(name)) {

                bl = delete(studentList.get(i));
            }
        }
        return bl;


    }

    public Integer handleCountMan(List<Student> studentList) {
        int count = 0;
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getSex().equals("男")) {
                count++;
            }
        }
        return count;
    }

    public Integer handleCountWomen(List<Student> studentList) {
        int count = 0;
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getSex().equals("女")) {
                count++;
            }
        }
        return count;
    }
}
